﻿using System.Text.Json;

namespace Domain.Repository.ResponseModels
{
    public abstract class NonStandardObject<T> where T : class
    {
        protected abstract object Obj { get; }

        public virtual IEnumerable<T> TLlist
        {
            get
            {
                var jsonStr = JsonSerializer.Serialize(Obj);

                var jdoc = JsonDocument.Parse(jsonStr);

                List<T> rlt;

                if (jdoc.RootElement.ValueKind == JsonValueKind.Array)
                {
                    rlt = JsonSerializer.Deserialize<List<T>>(jsonStr ?? "[]");
                }
                else
                {
                    var jobject = JsonSerializer.Deserialize<T>(jsonStr);
                    List<T>? result = null;
                    if (jobject != null)
                    {
                        result = new List<T>();
                        result.Add(jobject);
                    }
                    rlt = result;
                }
                return rlt;
            }
        }
    }
}
